<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>校园二手市场 - 我发布的商品</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }

        body {
            line-height: 1.6;
            color: #333;
            background-color: #f4f4f4;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .header {
            background-color: #4CAF50;
            color: white;
            padding: 1rem;
            text-align: center;
        }

        .content {
            flex: 1;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 1rem;
        }

        .product-list {
            background-color: #fff;
            padding: 1rem;
            border-radius: 4px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .product-card {
            margin-bottom: 1rem;
            border: 1px solid #ddd;
            padding: 1rem;
            border-radius: 4px;
            transition: transform 0.2s;
        }

        .product-card:hover {
            transform: translateY(-2px);
        }

        .product-card img {
            width: 100%;
            height: 200px;
            object-fit: cover;
            margin-bottom: 0.5rem;
            cursor: pointer;
            transition: opacity 0.2s;
        }

        .product-card img:hover {
            opacity: 0.9;
        }

        .product-card h3 {
            margin-bottom: 0.5rem;
        }

        .product-card p {
            margin-bottom: 0.2rem;
        }

        .nav-bar {
            display: flex;
            justify-content: space-around;
            background-color: #fff;
            padding: 0.5rem;
            position: fixed;
            bottom: 0;
            width: 100%;
            margin: 0 auto;
            left: 50%;
            transform: translateX(-50%);
            box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
            border-radius: 12px 12px 0 0;
            z-index: 102;
        }

        .nav-bar a {
            color: #666;
            text-decoration: none;
            padding: 0.5rem 1rem;
            display: flex;
            flex-direction: column;
            align-items: center;
            font-size: 0.8rem;
            transition: color 0.3s;
            position: relative;
        }

        .nav-bar a.active {
            color: #4CAF50;
        }

        .nav-bar a.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60%;
            height: 3px;
            background-color: #4CAF50;
            border-radius: 3px;
        }

        .nav-icon {
            width: 24px;
            height: 24px;
            position: relative;
            margin-bottom: 0.2rem;
        }

        .home-icon::before { content: "🏠"; }
        .products-icon::before { content: "📦"; }
        .publish-icon::before { content: "✏️"; }
        .news-icon::before { content: "💬"; }
        .account-icon::before { content: "👤"; }

        /* 新增：无商品状态样式 */
        .no-products-container {
            text-align: center;
            padding: 2rem 1rem;
        }

        .no-products {
            font-size: 1.1rem;
            color: #666;
            margin-bottom: 1rem;
        }

        .action-button {
            display: inline-block;
            padding: 0.8rem 2rem;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 24px;
            font-size: 1.1rem;
            font-weight: 500;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            cursor: pointer;
            border: none;
        }

        .action-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
        }

        /* 新增：图片模态框样式 */
        .image-modal {
            display: none;
            position: fixed;
            z-index: 200;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
            justify-content: center;
            align-items: center;
        }

        .image-modal.active {
            display: flex;
        }

        .modal-content {
            max-width: 90%;
            max-height: 90%;
            position: relative;
        }

        .modal-image {
            max-width: 100%;
            max-height: 85vh;
            object-fit: contain;
            animation-name: zoom;
            animation-duration: 0.3s;
        }

        .close-modal {
            position: absolute;
            top: -40px;
            right: 0;
            color: #f1f1f1;
            font-size: 35px;
            font-weight: bold;
            transition: 0.3s;
            cursor: pointer;
        }

        .close-modal:hover,
        .close-modal:focus {
            color: #bbb;
            text-decoration: none;
        }

        @keyframes zoom {
            from { transform: scale(0.9); }
            to { transform: scale(1); }
        }
    </style>
</head>
<body>
    <header class="header">
        <h1>校园二手市场 - 我发布的商品</h1>
    </header>

    <div class="content">
        <main class="container">
            <section class="product-list">
                <h2>我发布的商品列表</h2>
                <div id="my-products-list" class="product-grid">
                    <!-- 商品将通过JavaScript动态加载 -->
                </div>
            </section>
        </main>
    </div>

    <nav class="nav-bar">
        <a href="index.html">
            <div class="nav-icon home-icon"></div>
            首页
        </a>
        <a href="products.html">
            <div class="nav-icon products-icon"></div>
            商品
        </a>
        <a href="publish.html" class="active">
            <div class="nav-icon publish-icon"></div>
            发布
        </a>
        <a href="news.html">
            <div class="nav-icon news-icon"></div>
            消息
        </a>
        <a href="account.html">
            <div class="nav-icon account-icon"></div>
            我的
        </a>
    </nav>

    <!-- 新增：图片模态框 -->
    <div id="imageModal" class="image-modal">
        <div class="modal-content">
            <span class="close-modal">&times;</span>
            <img id="modalImage" class="modal-image" src="" alt="商品图片">
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const currentUser = JSON.parse(localStorage.getItem('currentUser'));
            if (!currentUser) {
                window.location.href = 'login.html';
            }

            const myProductsList = document.getElementById('my-products-list');
            const products = JSON.parse(localStorage.getItem('products')) || [];
            const myProducts = products.filter(product => product.userId === currentUser.id);

            if (myProducts.length === 0) {
                myProductsList.innerHTML = `
                    <div class="no-products-container">
                        <p class="no-products">您还没有发布任何商品，快来发布吧！</p>
                        <a href="publish.html" class="action-button">立即发布</a>
                    </div>
                `;
            } else {
                myProducts.forEach(product => {
                    const productCard = document.createElement('div');
                    productCard.className = 'product-card';
                    productCard.innerHTML = `
                        <img src="${product.image}" alt="${product.title}" data-id="${product.id}">
                        <h3>${product.title}</h3>
                        <p>${product.description}</p>
                        <p class="price">¥${product.price}</p>
                    `;
                    myProductsList.appendChild(productCard);
                });

                // 图片点击事件绑定
                const productImages = document.querySelectorAll('.product-card img');
                const imageModal = document.getElementById('imageModal');
                const modalImage = document.getElementById('modalImage');
                const closeModal = document.querySelector('.close-modal');

                productImages.forEach(img => {
                    img.addEventListener('click', function() {
                        modalImage.src = this.src;
                        modalImage.alt = this.alt;
                        imageModal.classList.add('active');
                        document.body.style.overflow = 'hidden'; // 防止背景滚动
                    });
                });

                // 关闭模态框
                closeModal.addEventListener('click', function() {
                    imageModal.classList.remove('active');
                    document.body.style.overflow = '';
                });

                // 点击模态框背景关闭
                imageModal.addEventListener('click', function(e) {
                    if (e.target === imageModal) {
                        imageModal.classList.remove('active');
                        document.body.style.overflow = '';
                    }
                });

                // ESC键关闭模态框
                document.addEventListener('keydown', function(e) {
                    if (e.key === 'Escape' && imageModal.classList.contains('active')) {
                        imageModal.classList.remove('active');
                        document.body.style.overflow = '';
                    }
                });
            }
        });
    </script>
</body>
</html>